package com.project.frba.bifront.client.view.dashboard;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.Checkbox;
import com.gwtext.client.widgets.form.Field;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.Radio;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.form.event.CheckboxListenerAdapter;
import com.gwtext.client.widgets.form.event.TextFieldListenerAdapter;
import com.gwtext.client.widgets.layout.FitLayout;
import com.project.frba.bifront.client.model.Dashboard;
import com.project.frba.bifront.client.rpc.DashboardServices;
import com.project.frba.bifront.client.rpc.DashboardServicesAsync;
import com.project.frba.bifront.client.view.MessageWindow;

public class DashboardForm extends Composite {

	private Window mainWindow = new Window();
	
	private DashboardList dashboardList;
	
	private TextField tfName = new TextField("Nombre", "name", 160);
	
	private Dashboard dashboard = null;
	
	private Button btSave = new Button("Guardar");
	
	private Button btCancel = new Button("Cancelar");
	
	private Grid dualButtonsGrid = new Grid(1, 2);
	
	private Grid quadButtonsGrid = new Grid(2, 2);
	
	private Radio rDual = new Radio("Doble", "type");

	private Radio rQuad = new Radio("Cuadruple", "type");
	
	private Button btDualFirst = new Button("Eliminar 1");
	
	private Button btDualSecond = new Button("Eliminar 2");
	
	private Button btQuadFirst = new Button("Eliminar 1");
	
	private Button btQuadSecond = new Button("Eliminar 2");
	
	private Button btQuadThird = new Button("Eliminar 3");
	
	private Button btQuadFourth = new Button("Eliminar 4");
	
	private HorizontalPanel dualButtonsPanel = new HorizontalPanel();
	
	private HorizontalPanel quadButtonsPanel = new HorizontalPanel();
	
	
	public DashboardForm(Long id, Integer type, DashboardList dashboardList) {

		
		this.dashboardList = dashboardList;
		if (id != 0) {
			//TODO Deshabilitar cambiar el tipo una vez que se esta editando.
			
			rQuad.setDisabled(true);
			
			
			AsyncCallback<Dashboard> callback = new AsyncCallback<Dashboard>() {
				public void onSuccess(Dashboard dashboardResult) {
					dashboard = dashboardResult;
					if(dashboard.getType().equals(Dashboard.DUAL)){
						rQuad.setDisabled(true);
						rDual.setChecked(true);
					} else {
						rDual.setDisabled(true);
						rQuad.setChecked(true);
					}
					loadActions();
					loadForm();
				}
				
				public void onFailure(Throwable caught) {
					new MessageWindow("No se ha podido obtener el tablero.");					
				}
			};
			DashboardServicesAsync dashboardServices = (DashboardServicesAsync)GWT.create(DashboardServices.class);
			dashboardServices.findDashboardById(id, type, callback);
		} else {
			dashboard = new Dashboard();
			dashboard.setId(id);
			dashboard.setName("");
			dashboard.setType(Dashboard.DUAL);
			loadActions();
			loadForm();
		}
		
		
	}
	
	public void loadForm(){
		mainWindow.setTitle("Tablero");
		mainWindow.setWidth(400);
		mainWindow.setHeight(260);
		mainWindow.setModal(true);
		mainWindow.setResizable(false);
		mainWindow.setLayout(new FitLayout());
		
		final FormPanel formPanel = new FormPanel();
		formPanel.setPaddings(4);
		
		tfName.setValue(dashboard.getName());
		rDual.setLabel("Tipo");
		formPanel.add(tfName);
		formPanel.add(rDual);
		formPanel.add(rQuad);
		
		
		
		
		dualButtonsPanel.setWidth("400");
		dualButtonsPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
		dualButtonsGrid.setWidget(0, 0, btDualFirst);
		dualButtonsGrid.setWidget(0, 1, btDualSecond);
		dualButtonsPanel.add(dualButtonsGrid);
		formPanel.add(dualButtonsPanel);
		
		quadButtonsPanel.setWidth("400");
		quadButtonsPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
		quadButtonsGrid.setWidget(0, 0, btQuadFirst);
		quadButtonsGrid.setWidget(0, 1, btQuadSecond);
		quadButtonsGrid.setWidget(1, 0, btQuadThird);
		quadButtonsGrid.setWidget(1, 1, btQuadFourth);
		quadButtonsPanel.add(quadButtonsGrid);
		formPanel.add(quadButtonsPanel);
		
		
		btDualFirst.getElement().setClassName("btRemoveDashboardImage");
		btDualSecond.getElement().setClassName("btRemoveDashboardImage");
		
		btQuadFirst.getElement().setClassName("btRemoveDashboardImage");
		btQuadSecond.getElement().setClassName("btRemoveDashboardImage");
		btQuadThird.getElement().setClassName("btRemoveDashboardImage");
		btQuadFourth.getElement().setClassName("btRemoveDashboardImage");
		
		mainWindow.add(formPanel);
		mainWindow.setButtons(new Button[]{btSave, btCancel});
		initWidget(mainWindow);
		mainWindow.show();
		
		
		//cargar radio marcado y deshabilitar botones
		if(dashboard.getType().equals(Dashboard.DUAL)) {
			rDual.setChecked(true);
			if (dashboard.getFirst() == null) {
				btDualFirst.setDisabled(true);
			}
			
			if (dashboard.getSecond() == null) {
				btDualSecond.setDisabled(true);
			}
			btQuadFirst.setDisabled(true);
			btQuadSecond.setDisabled(true);
			btQuadThird.setDisabled(true);
			btQuadFourth.setDisabled(true);
		} else {
			rDual.setChecked(false);
			if (dashboard.getFirst() == null) {
				btQuadFirst.setDisabled(true);
			}
			
			if (dashboard.getSecond() == null) {
				btQuadSecond.setDisabled(true);
			}
			
			if (dashboard.getThird() == null) {
				btQuadThird.setDisabled(true);
			}
			
			if (dashboard.getFourth() == null) {
				btQuadFourth.setDisabled(true);
			}
			
		}
	}

	private void loadActions() {
		btSave.addListener(new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				storeDashboard();
			}
		});
		
		btCancel.addListener(new ButtonListenerAdapter() {
			public void onClick(Button button, EventObject e) {
				mainWindow.close();
			}
		});
		
		rDual.addListener(new CheckboxListenerAdapter(){
			public void onCheck(Checkbox field, boolean checked) {
				dualButtonsPanel.setVisible(checked);
				quadButtonsPanel.setVisible(!checked);
				if(checked){
					dashboard.setType(Dashboard.DUAL);
				} else {
					dashboard.setType(Dashboard.QUAD);
				}
			}
		});
		
		btDualFirst.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				dashboard.setFirst(null);
				btDualFirst.setDisabled(true);
			}
		});
		
		btDualSecond.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				dashboard.setSecond(null);
				btDualSecond.setDisabled(true);
			}
		});
		
		btQuadFirst.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				dashboard.setFirst(null);
				btQuadFirst.setDisabled(true);
			}
		});
		
		btQuadSecond.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				dashboard.setSecond(null);
				btQuadSecond.setDisabled(true);
			}
		});
		
		btQuadThird.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				dashboard.setThird(null);
				btQuadThird.setDisabled(true);
			}
		});
		
		btQuadFourth.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				dashboard.setFourth(null);
				btQuadFourth.setDisabled(true);
			}
		});
		
		tfName.addListener(new TextFieldListenerAdapter(){
			public void onBlur(Field field) {
				dashboard.setName(field.getValueAsString());
			}
		});
	}
	
	private void storeDashboard() {
		AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>(){
			public void onFailure(Throwable caught) {
				new MessageWindow("No se ha podido guardar el tablero");
			}
			
			public void onSuccess(Boolean result) {
				dashboardList.reload();
				mainWindow.close();
			}
		};
		
		DashboardServicesAsync dashboardServices = (DashboardServicesAsync)GWT.create(DashboardServices.class);
		dashboardServices.storeDashboard(dashboard, callback);
	}
}
